package com.graduate.cec.controller;


import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.JsonResult;
import com.graduate.cec.pojo.dto.AdminLoginDTO;
import com.graduate.cec.pojo.dto.AdminSimplePageDTO;
import com.graduate.cec.pojo.dto.LoginDTO;
import com.graduate.cec.pojo.entity.Admin;
import com.graduate.cec.pojo.entity.User;
import com.graduate.cec.pojo.vo.AdminSimpleVO;
import com.graduate.cec.pojo.vo.AdminVO;
import com.graduate.cec.service.AdminRoleService;
import com.graduate.cec.service.AdminService;
import com.graduate.cec.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping(value = "/admin",produces = "application/json;charset=utf-8")
@Api(tags = "管理员")
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private AdminRoleService adminRoleService;

    @Resource
    private LoginService login;
    /****************        增       ******************************************************************/

    //上传图片
    @PostMapping("/upload-picture")
    @ApiOperation("上传图片")
    public JsonResult<String> upload(MultipartFile picFile) throws Exception {
        String fileName = adminService.uploadPictures(picFile);
        return JsonResult.ok("插入成功",fileName);

    }



    @PostMapping("/admin-register")
    public JsonResult<Void> insertRegisterUser(@RequestBody Admin admin) {
        Long register = adminService.register(admin);

        adminRoleService.insertRegister(register);

        return JsonResult.ok("注册成功");
    }



    /****************        删       ******************************************************************/
    //删除上传的图片
    @GetMapping("/remove-picture")
    @ApiOperation("删除图片")
    public JsonResult<Void> remove(String pictureName) {
        adminService.removePicture(pictureName);
        return JsonResult.ok("删除成功");
    }



    /****************        改       ******************************************************************/
    @PostMapping("/update-info")
    @ApiOperation("修改用户信息")
    public JsonResult<Void> updateInfo(@RequestBody Admin admin) {
        adminService.updateInfo(admin);
        return JsonResult.ok("修改成功");
    }


    /****************        查        ******************************************************************/

    /**
     *
     * @param loginDTO 登录
     * @return String
     */
    @PostMapping("/login")
    @ApiOperation("查询全部列表")
    public JsonResult<String> login(@RequestBody LoginDTO loginDTO){
        return JsonResult.ok("查询成功",login.login(loginDTO,"管理员"));
    }

    @GetMapping("/personal")
    @ApiOperation("根据电话查询信息")
    public JsonResult<AdminSimpleVO> selectSimplePersonal() {

        return JsonResult.ok(adminService.selectSimplePersonal());
    }


    @GetMapping("/list")
    @ApiOperation("查询所有管理员")
    public JsonResult<JsonPage<AdminVO>> selectAllUser(AdminSimplePageDTO adminSimplePageDTO) {
        JsonPage<AdminVO> adminJsonPage = adminService.selectAllAdmin(adminSimplePageDTO);

        return JsonResult.ok("查询成功",adminJsonPage);
    }



}
